import json
import re
from collections import OrderedDict

camelize_re = re.compile(r"[a-z0-9]?_[a-z0-9]")


def underscore_to_camel(match):
    group = match.group()
    if len(group) == 3:
        return group[0] + group[2].upper()
    else:
        return group[1].upper()


def camelize(data, **options):
    # Handle lazy translated strings.
    ignore_fields = options.get("ignore_fields") or ()
    ignore_keys = options.get("ignore_keys") or ()

    if isinstance(data, dict):

        new_dict = OrderedDict()
        for key, value in data.items():

            if isinstance(key, str) and "_" in key:
                new_key = re.sub(camelize_re, underscore_to_camel, key)
            else:
                new_key = key

            if key not in ignore_fields and new_key not in ignore_fields:
                result = camelize(value, **options)
            else:
                result = value
            if key in ignore_keys or new_key in ignore_keys:
                new_dict[key] = result
            else:
                new_dict[new_key] = result
        return new_dict
    if is_iterable(data) and not isinstance(data, str):
        return [camelize(item, **options) for item in data]
    return data


def get_underscoreize_re(options):
    if options.get("no_underscore_before_number"):
        pattern = r"([a-z0-9]|[A-Z]?(?=[A-Z](?=[a-z])))([A-Z])"
    else:
        pattern = r"([a-z0-9]|[A-Z]?(?=[A-Z0-9](?=[a-z0-9]|(?<![A-Z])$)))([A-Z]|(?<=[a-z])[0-9](?=[0-9A-Z]|$)|(?<=[A-Z])[0-9](?=[0-9]|$))"
    return re.compile(pattern)


def camel_to_underscore(name, **options):
    underscoreize_re = get_underscoreize_re(options)
    return underscoreize_re.sub(r"\1_\2", name).lower()


def _get_iterable(data):
    return data.items()


def underscoreize(data, **options):
    ignore_fields = options.get("ignore_fields") or ()
    ignore_keys = options.get("ignore_keys") or ()
    if isinstance(data, dict):
        new_dict = {}

        for key, value in _get_iterable(data):
            if isinstance(key, str):
                new_key = camel_to_underscore(key, **options)
            else:
                new_key = key

            if key not in ignore_fields and new_key not in ignore_fields:
                result = underscoreize(value, **options)
            else:
                result = value
            if key in ignore_keys or new_key in ignore_keys:
                new_dict[key] = result
            else:
                new_dict[new_key] = result

        return new_dict
    if is_iterable(data):
        return [underscoreize(item, **options) for item in data]

    return data


def is_iterable(obj):
    if isinstance(obj, str):
        return False
    try:
        iter(obj)
    except TypeError:
        return False
    else:
        return True


if __name__ == "__main__":
    d = """
    {
  "expiredTime": 1710119798,
  "expiration": "2024-03-11T01:16:38Z",
  "credentials": {
    "sessionToken": "mQ1WO0c84tpZ9tdQmY8PEIBGJbFYgI8a350ddc108ba27365e4574c44286870b1FWFys3sravtHKtPNsp8fVTZ8br7J7GAa3y_nlKy2HMoFEZzW4G23E4W5mZgUZi7w7fkEgIPXDs5AKUct_Xf2Bbv-0ejX2vPCVSe1SVOAEjrSut0o69D8mfSXAH6HA5ZMI57W858EoJ-DcR3l7HqYbtUJaNotSUpM3wyERskwJ7KtSL9qov1jWBqq5ZHDyGqlV1PP2HHngcKdJ9dWxK78BtDLWpC5wCGQ7UL3blcJ2xFx32O9B4noKin7aDrIs5fuAHZHORmJ5BP0k27SDSButAf4XF_tAJNGiH3PIZZiLsp0XthoF5tCJ__WK3S_XzesZM1pBQW24pcRWod3iQFgDpe4218J2Pp44SGmf9POQKPzjtfmyNcD9ll_fzqvpRjNHjHXWgp7okyuxYJVed2kIP_Ntbe8SaR7RIEwgEWmQbq1pRD8SC6YM9ZPDF_bJMSn7o-fNrVRNYUbA8y3pERUPVqYxhyA_gb5_U1B-cwNktREbJy3ofkB9LUOcMK8UeX236ngbodM7JUP898S7y7aDmqSHJFZjAoDxYrNuXYs4OdqCYAASrhG12yA5jvzAPGEXXpMSCgRNCikBBIXHrdJf6I0P9o3mOWBRJzTui20smxzmonocQpg69uQopS_qRa1H5XQr7Hlk6txgNp7S7q9wUJ4yn8KXz9HLELjj8gNANDXuSsAYYVQIwDW0lEgBscSAtrfXiPPxHT3Lc_-lGpjyAQb_K3mR9taLG0Ynsh5-XA",
    "tmpSecretId": "AKIDL3P72aQ5qMZVawjTjh19r2NUsoqAsT3PtnHX-v5ijXlEA3YZNAtbrtlMsZ9bL71U",
    "tmpSecretKey": "Unw2hboS5l8Q0zIdOzXJAqWw/LLPPWueMXSoVCNsD90="
  },
  "requestId": "c3faf5e6-6849-4e3c-aefd-509e1f2ee684",
  "startTime": 1710117998,
  "bucket": "venus-test-1251808174",
  "region": "ap-shanghai",
  "prefix": "media/dashed/tk/87/47/6768747/*"
}
    """

    secret = underscoreize(json.loads(d))
    credential = secret.get('credentials')
    secret.get('region', 'ap-shanghai'),
    SecretId = credential.get("tmp_secret_id"),
    SecretKey = credential.get("tmp_secret_key"),
    Token = credential.get("session_token"),
    print('ok')
